package com.braveman.im.ui.activity.message;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.braveman.im.R;
import com.braveman.im.bean.DataListBean;
import com.braveman.im.bean.GroupMemberBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.adapter.GroupListMemberAdapter;
import com.braveman.im.utils.UtilsStyle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class GroupMemberActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {

    private String groupId;
    private RecyclerView mLvActivityListMember;
    private GroupListMemberAdapter groupListMemberAdapter;
    private List<DataListBean> groupMemberBeanList = new ArrayList<>();
    private LoadingLayout loadingLayout;
    private SwipeRefreshLayout mRefreshLayout;
    private int pageNo = 1;
    private boolean isLastPage = true;

    @Override
    protected void initIntentData(Intent callIntent) {
        groupId = callIntent.getStringExtra("groupId");
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("群成员列表");
    }

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_group_member;
    }

    @Override
    protected void initChildView() {
        loadingLayout = (LoadingLayout) findViewById(R.id.loading_layout);
        mLvActivityListMember =  findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
    }

    @Override
    protected void initChildData() {
        groupListMemberAdapter = new GroupListMemberAdapter(R.layout.item_group_member, groupMemberBeanList);
        mLvActivityListMember.setAdapter(groupListMemberAdapter);
        doNetwork();

    }

    private void doNetwork() {
        loadingLayout.showLoading();
        ApiManager.getApi().getGroupMemberList(groupId, pageNo, 10).enqueue(new NetCallBack<GroupMemberBean>() {
            @Override
            protected void onSuccess(Call<GroupMemberBean> call, Response<GroupMemberBean> response) {
                if (response != null) {
                    GroupMemberBean groupMemberBean = response.body();
                    isLastPage = groupMemberBean.isLastPage;
                    if (pageNo == 1 && groupMemberBeanList != null) {
                        groupMemberBeanList.clear();

                    }
                    groupMemberBeanList.addAll(groupMemberBean.dataList);
                    if (groupListMemberAdapter != null) {
                        groupListMemberAdapter.notifyDataSetChanged();
                    }

                    if (groupMemberBeanList.size() == 0) {
                        loadingLayout.showEmpty();
                    } else {
                        loadingLayout.showContent();
                    }
                    mRefreshLayout.setRefreshing(false);
                } else {

                    loadingLayout.showError();
                    mRefreshLayout.setRefreshing(false);
                }
            }

            @Override
            public void onFailure(Call<GroupMemberBean> call, Throwable t) {
                super.onFailure(call, t);
                loadingLayout.showError();
                mRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        groupListMemberAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("username", groupMemberBeanList.get(position).memberName);
                setResult(RESULT_OK, intent);
                ActivityUtils.finishActivity(GroupMemberActivity.this);
            }
        });

        loadingLayout.setRetryListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pageNo = 1;
                doNetwork();
            }
        });
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                doNetwork();
            }
        });

        groupListMemberAdapter.setOnLoadMoreListener(this, mLvActivityListMember);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

    }


    @Override
    public void onLoadMoreRequested() {
        pageNo++;
        if (isLastPage) {
            groupListMemberAdapter.loadMoreEnd();
        } else {
            doNetwork();
        }
    }
}
